﻿<UserControl x:Class="ObjectTree"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:RawExplorer2.UI"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:tokenlib="clr-namespace:RawExplorer2.TokenLib;assembly=TokenLib"
             x:Name="objectTree"
             SnapsToDevicePixels="True"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
	<UserControl.Resources>
		<ResourceDictionary>
			<local:BitmapConverter x:Key="BitmapConverter" />
			<local:NullImageConverter x:Key="NullImageConverter" />
			<!--
				<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="..\ControlTemplates\TreeViewItemTemplate.xaml" />
				</ResourceDictionary.MergedDictionaries>
			-->
			<local:BindingProxy x:Key="DataContextProxy" Data="{Binding ElementName=objectTree}" />
			<ControlTemplate x:Key="MenuSeperator">
				<Separator />
			</ControlTemplate>
			<local:AutoGreyableImage x:Key="ContextMenuImage"
			       Width="16"
			       Height="16"
			       Source="{Binding ImagePath,
			                        Converter={StaticResource NullImageConverter}}"
			       x:Shared="False" />
			<local:ZeroToFalseConverter x:Key="EnableContextMenuConverter" />
			<local:RawObjectToImageConverter x:Key="RawObjectToImageConverter" />
		</ResourceDictionary>
	</UserControl.Resources>
	<Grid>
		<TreeView Grid.Column="0"
		          ContextMenuService.IsEnabled="{Binding Path=Items.Count,
		                                                 ElementName=ObjectsContextMenu,
		                                                 Converter={StaticResource EnableContextMenuConverter}}"
		          ItemsSource="{Binding Path=ObjectTypeNodes,
		                                ElementName=objectTree}"
		          PreviewMouseRightButtonDown="TreeView_PreviewMouseRightButtonDown"
		          VirtualizingPanel.IsVirtualizing="True"
		          VirtualizingPanel.ScrollUnit="Pixel"
		          VirtualizingPanel.VirtualizationMode="Recycling">
			<TreeView.ContextMenu>
				<ContextMenu x:Name="ObjectsContextMenu" ItemsSource="{Binding Path=Data.ContextMenuItems, Source={StaticResource DataContextProxy}}">
					<ContextMenu.ItemContainerStyle>
						<Style TargetType="MenuItem">
							<Setter Property="Header" Value="{Binding Text}" />
							<Setter Property="Icon" Value="{StaticResource ContextMenuImage}" />
							<Setter Property="Command" Value="{Binding Command}" />
							<Style.Triggers>
								<DataTrigger Binding="{Binding}" Value="{x:Null}">
									<Setter Property="Template" Value="{StaticResource MenuSeperator}" />
								</DataTrigger>
							</Style.Triggers>
						</Style>
					</ContextMenu.ItemContainerStyle>
				</ContextMenu>
			</TreeView.ContextMenu>
			<TreeView.ItemContainerStyle>
				<Style TargetType="{x:Type TreeViewItem}">
					<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
					<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
				</Style>
			</TreeView.ItemContainerStyle>
			<TreeView.Resources>
				<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}" />
				<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}" />
				<HierarchicalDataTemplate DataType="{x:Type local:TypeNodeVm}" ItemsSource="{Binding Path=Objects}">
					<StackPanel Margin="1"
					            HorizontalAlignment="Stretch"
					            Orientation="Horizontal">
						<Image>
							<Image.Style>
								<Style TargetType="{x:Type Image}">
									<Setter Property="Source" Value="..\..\Icons\FolderClosed.png" />
									<Style.Triggers>
										<DataTrigger Binding="{Binding Path=IsExpanded}" Value="True">
											<Setter Property="Source" Value="..\..\Icons\FolderOpen.png" />
										</DataTrigger>
									</Style.Triggers>
								</Style>
							</Image.Style>
						</Image>
						<TextBlock Text="{Binding Path=Name}" />
					</StackPanel>
				</HierarchicalDataTemplate>

				<HierarchicalDataTemplate DataType="{x:Type local:ObjectNodeVm}">
					<StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
						<Image Source="{Binding Path=RawObject, Converter={StaticResource RawObjectToImageConverter}}" />
						<TextBlock Margin="3,0,0,0" Text="{Binding Path=RawObject.ObjectId}" />
					</StackPanel>
				</HierarchicalDataTemplate>
			</TreeView.Resources>
		</TreeView>

	</Grid>
</UserControl>
